#!/usr/bin/python
import re
import time

ref = {}
LOG_PATH = '/var/log/squid3/access.log'

def readIpList():
	for line in open('iplist.txt'):
		data = re.compile("\s*").split(line)
		nip = data[0].split('.')
		ipint =  int(nip[0]) * (256 ** 3) + int(nip[1]) * (256 ** 2) + int(nip[2]) * 256 + int(nip[3])
		ref[ipint] = [data[0], data[2]]
	global refLength
	refLength = len(ref)
	global refKeys
	refKeys = sorted(ref)
	
def isFree(ip):
	if ip == '-':
		return True		#why.........
	ipData = ip.split('.')
	if len(ipData) != 4:
		return True
	for i in range(4):
		if not ipData[i].isdigit():
#			print ip
			return True		#why....
	ipint =  int(ipData[0]) * (256 ** 3) + int(ipData[1]) * (256 ** 2) + int(ipData[2]) * 256 + int(ipData[3])
	k = -1
	"""
	for key in sorted(ref):
		if ipint > key :
			k = key
#			print key
			continue
		break
	"""
	i = 0
	j = refLength - 1
#	print ipint
	while i < j:
		m = (i + j) / 2
		networkInt = refKeys[m]
#		print i, m, j, refKeys[m]
		if networkInt == ipint: return True
		if ipint > networkInt and m < refLength and refKeys[m + 1] > ipint: 
			k = networkInt
			break
		if i == m: 
			if ipint > networkInt:
				k = refKeys[j]
			break
		if ipint > networkInt: 
			i = m
		else: 
			j = m
		
	isFree = False
	if k != -1:
		hostData = ref[k][0].split('.')
		maskData = ref[k][1].split('.')
		isFree = True
		for i in range(4):
			if int(hostData[i]) & int(maskData[i]) != int(ipData[i]) & int(maskData[i]): isFree = False
	return isFree

def format(num):
	if num < 1024 : return str(num) + 'B'
	elif num < 1024 ** 2 : return str(num / 1024) + 'KB'
	elif num < 1024 ** 3 : return str(num / 1024 ** 2) + 'MB'
	return str(num / 1024 ** 3) + 'MB'

stat = {}
readIpList()

f = open(LOG_PATH)
lineCount = f.read().count('\n')
f.seek(0)
count = 0
print 'Ready to go!'
for line in f:
	cols = re.compile("\s*").split(line)
	resultCode = cols[3]
	bytes = int(cols[4])
	peerIP = cols[8].split('/')[1]
	_time = time.gmtime(float(cols[0]))
	if not _time[0] in stat : stat[_time[0]] = {}
	if not _time[1] in stat[_time[0]] : stat[_time[0]][_time[1]] = {'free': 0, 'nonFree': 0, 'cached': 0}
	cacheHit = resultCode.find("HIT") != -1
	if cacheHit:
		stat[_time[0]][_time[1]]['cached'] += bytes
	elif isFree(peerIP):
		stat[_time[0]][_time[1]]['free'] += bytes
	else:
		stat[_time[0]][_time[1]]['nonFree'] += bytes
	count += 1
	print '\b\r' + str(count * 100 / lineCount) + '% (' + str(count) + '/' + str(lineCount) + ') done'
		
print 'Month\tNone-Free\tFree\tCached'
for y in stat.keys():
	print str(y) + '\n_________________________________________________'
	for m in sorted(stat[y]):
		print m, '\t', format(stat[y][m]['nonFree']), '\t', format(stat[y][m]['free']), '\t', format(stat[y][m]['cached'])

